import {Injectable} from '@angular/core';
import {ExecActionInterface} from "./exec-action-interface";
import {FileInfo} from "../model/file-info";
import {ParamDef} from "../model/param-def";
import {ParamDefType} from "../model/param-def-type";
import {SystemUtilsService} from "../service/system-utils.service";

@Injectable({
  providedIn: 'root'
})
export class FileExecActionService extends ExecActionInterface {
  public readonly type:ParamDef;
  public readonly option:ParamDef;
  public readonly dirName:ParamDef;


  constructor(protected system:SystemUtilsService) {
    super("文件操作", "file");
    this.type = new ParamDef(ParamDefType.Select, "type")
    this.option= new ParamDef(ParamDefType.Select,"option")
    this.dirName= new ParamDef(ParamDefType.Text,"dirName")

    this.type.addNameValue("复制文件","copyFile")
      .subParamDefs=[this.option]

    this.type.addNameValue("移动文件","moveFile")
      .subParamDefs=[this.option]

    this.type.addNameValue("添加目标子目录","addDir","默认取上次操作结果")
      .subParamDefs=[this.dirName]

    this.paramsDef=[this.type]


    this.option.addNameValue("已存在忽略","ignore")
    this.option.addNameValue("已存在覆盖","override")
    this.option.addNameValue("已存在重命名","rename")
  }

  async exec(fileInfo: FileInfo, lastVal: any, param: any):Promise<any> {
    let type  = param.type

    switch (type){
      case "copyFile":
      case "moveFile":
      {
        let targetPath = this.system.pathJoin(fileInfo.targetDir)
        if (fileInfo.config!=null && fileInfo.config.targetDir!=null ){
          targetPath = this.system.pathJoin([fileInfo.config.targetDir,targetPath])
        }
        let targetFile = this.system.pathJoin([targetPath,fileInfo.targetName?fileInfo.targetName:fileInfo.name])

        this.system.info(`[${type}] => ${targetFile}`)
        //todo
        break
      }
      case "addDir":{
        if (param.dirName == null || param.dirName == ''){
          fileInfo.targetDir.push(lastVal)
        }else{
          fileInfo.targetDir.push(param.dirName)
        }
        return true;
      }
    }




  }

}
